package cn.eoeit.alone.phoneImport.db;


import java.util.ArrayList;
import java.util.List;

import cn.eoeit.alone.phoneImport.model.CityBean;
import cn.eoeit.alone.phoneImport.model.PhoneRecord;
import cn.eoeit.alone.phoneImport.util.LogUtil;
import cn.eoeit.alone.phoneImport.util.PreferenceUtil;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;



public class CacheDbAgentImpl extends AbstractDatabaseAgent implements
		CacheDbAgent {
	private static final String TAG = LogUtil.getTag(CacheDbAgentImpl.class);
	/*** 查询的base SQL */
	private static final String BASE_QUERY_SQL = "select [COLUMES] from ";
	

	private Context mContext;

	private CacheDbOpenHelper mHelper;

	@Override
	protected SQLiteDatabase openSQLiteDatabase() {
		return mHelper.getWritableDatabase();
	}

	public CacheDbAgentImpl(Context context) {
		LogUtil.i(TAG, "实例化CacheDbAgentImpl");
		mContext = context;
		String iscopy=PreferenceUtil.getStrValue(mContext, "copyDataBase", "0");
        if("0".equals(iscopy)){
        	LogUtil.e(TAG, iscopy+"-- Database onCreate --");
        	//FileUtil.copyDatabaseFile(mContext, true);  
        	 PreferenceUtil.saveStrValue(mContext, "copyDataBase", "1");     
        }
		mHelper = new CacheDbOpenHelper(mContext);
	}

	@Override
	public int insert(final DatabaseBean bean) {
		SqlTask<Integer> task = new SqlTask<Integer>() {
			@Override
			protected Integer doExecute() {
				long rowId = -1;
				String table = bean.getTableName();
				ContentValues values = bean.toContentValues();
				if (table != null ) {
					if(values.containsKey(CacheDbAgent.COLUMN_ID))
					{
						values.remove(CacheDbAgent.COLUMN_ID);
					}
					LogUtil.i(TAG, "INSERT INTO " + table + " VALUES " + values);
					rowId = db.insert(table, null, values);
				}
				return (int) rowId;
			}
		};
		if (bean == null) {
			LogUtil.w(TAG, "bean is NULL!");
			return 0;
		} else {
			return task.execute();
		}
	}

	@Override
	public boolean delete(final DatabaseBean bean) {
		SqlTask<Boolean> task = new SqlTask<Boolean>() {
			@Override
			protected Boolean doExecute() {
				int rowCount = -1;
				String table = bean.getTableName();
				String id = Integer.toString(bean.getId());
				if (table != null && !TextUtils.isEmpty(id)) {
					LogUtil.d(TAG, "DELETE " + table + " FROM id = " + id);
					String whereClause = "id = ?";
					String[] whereArgs = new String[] { id };
					rowCount = db.delete(table, whereClause, whereArgs);
				}

				return rowCount > 0;
			}
		};

		if (bean == null) {
			LogUtil.w(TAG, "bean is NULL!", new Exception());
			return false;
		} else {
			return task.execute();
		}

	}

	@Override
	public boolean update(final DatabaseBean bean) {
		SqlTask<Boolean> task = new SqlTask<Boolean>() {
			@Override
			protected Boolean doExecute() {
				int rowCount = -1;
				String table = bean.getTableName();
				ContentValues values = bean.toContentValues();
				String whereClause = null;
				String[] whereArgs = null;
				if (table != null && values.containsKey(CacheDbAgent.COLUMN_ID)) {
					LogUtil.i(TAG, "UPDATE " + table + " VALUES " + values);
					String id = values.getAsString(CacheDbAgent.COLUMN_ID);
					values.remove(CacheDbAgent.COLUMN_ID);
					whereClause = CacheDbAgent.COLUMN_ID+"=?";
					whereArgs = new String[] { id };
					rowCount = db.update(table, values, whereClause, whereArgs);
				}
				return rowCount > 0;
			}
		};
		if (bean == null) {
			LogUtil.w(TAG, "bean is NULL!", new Exception());
			return false;
		} else {
			return task.execute();
		}
	}

	@Override
	public ArrayList<CityBean> findAllCityBean() {
SqlTask<ArrayList<CityBean>> task = new SqlTask<ArrayList<CityBean>>() {
			
			@Override
			protected ArrayList<CityBean> doExecute() {
				ArrayList<CityBean>list = new ArrayList<CityBean>();
				CityBean model = null;
				String  sql=BASE_QUERY_SQL+ HistoryTable.TABLE_NAME ;
				sql = getSql(sql, HistoryTable.COLUMNS);
				Cursor cursor = rawQuery(sql);
				if(cursor != null && cursor.moveToFirst())
				{
					do {
						model = new CityBean(cursor);
						list.add(model);
					} while (cursor.moveToNext());
					cursor.close();
				}
				return list;
			}
		};
		return task.execute();
	}

	@Override
	public List<PhoneRecord> getPhoneRecord() {
		
		return null;
	}
	
}
